package com.example.data0306;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.util.SparseArray;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.data0306.Adapter.Left_MenuAdapter;
import com.example.data0306.Day01.Setting;
import com.example.data0306.base.BaseActivity;
import com.example.data0306.bean.Left_menuBean;
import com.example.data0306.news.MineFragment;
import com.example.data0306.news.NewTopFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;

public class MainActivity extends BaseActivity {

    private DrawerLayout mDrawerLayout;

    private View settingBtn;
    private View scanView;
    private ImageView userHeadView;
    private View moreView;
    private TextView userNameView;
    private TextView levelView;
    private TextView userSignatureView;
    private RecyclerView menuListView;
    private View rootContentView;
    private Left_MenuAdapter mLeftMenuAdapter;
    private View drawerBtn;
    private View top01;

    private DrawerLayout rootView;

    private BottomNavigationView mBottomNavigationView;

    private final SparseArray<Fragment> mFragmentCache = new SparseArray<>();

    @Override
    protected int contentView() {
        return R.layout.activity_main1;
    }

    @Override
    protected void initView() {

        mDrawerLayout = findViewById(R.id.drawer_layout);

        settingBtn = findViewById(R.id.ll_setting_btn);  //
        scanView = findViewById(R.id.img_scan_btn);      //
        userHeadView = findViewById(R.id.img_user_head1); //
        moreView = findViewById(R.id.img_more_btn);      //
        userNameView = findViewById(R.id.tv_user_nick);  //
        levelView = findViewById(R.id.tv_user_level);    //
        userSignatureView = findViewById(R.id.tv_signature);  //
        menuListView = findViewById(R.id.rv_menu_list);  //
        rootContentView = findViewById(R.id.view_main_content);
        drawerBtn = findViewById(R.id.img_drawer_btn);   //
        top01 = findViewById(R.id.top1);

        rootView = findViewById(R.id.drawer_layout1);
        //找到左边的头像布局
        ImageView view = findViewById(R.id.img_user_head1);
        //然后给头像设置一个新的图片
        view.setImageResource(R.drawable.icon_head_pic_3);

        mBottomNavigationView = findViewById(R.id.bottom_nav_view);

        findViewById(R.id.img_drawer_btn).setOnClickListener(new View.OnClickListener() {
            @SuppressLint("WrongConstant")
            @Override
            public void onClick(View view) {
                rootView.openDrawer(Gravity.START);
            }
        });
    }

    protected void initData() {
        menuListView.setLayoutManager(new LinearLayoutManager(getContext()));
        mLeftMenuAdapter = new Left_MenuAdapter();
        menuListView.setAdapter(mLeftMenuAdapter);

    }

    @Override
    protected void bindData() {
        super.bindData();
        userNameView.setText("Me");
        levelView.setText("Lv21");
        userSignatureView.setText("学习android");

        ArrayList<Left_menuBean> menuBeans = new ArrayList<Left_menuBean>();
        menuBeans.add(new Left_menuBean(R.drawable.ic_baseline_message_24, "我的评论"));
        menuBeans.add(new Left_menuBean(R.drawable.ic_baseline_mail_24, "与我相关"));
        menuBeans.add(new Left_menuBean(R.drawable.ic_baseline_assignment_24, "我的订单"));
        menuBeans.add(new Left_menuBean(R.drawable.ic_baseline_check_box_24, "我的任务"));
        menuBeans.add(new Left_menuBean(R.drawable.ic_baseline_loyalty_24, "我的收藏"));
        menuBeans.add(new Left_menuBean(R.drawable.ic_baseline_card_giftcard_24, "活动中心"));
        menuBeans.add(new Left_menuBean(R.drawable.ic_baseline_verified_user_24, "信用分"));
        mLeftMenuAdapter.addData(menuBeans);
        userHeadView.setImageResource(R.drawable.icon_head);
        mBottomNavigationView.setOnNavigationItemSelectedListener(item -> {
            Fragment fragment = getContent(item);
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.fl_main_content, fragment)
                    .commit();
            return true;
        });
        mBottomNavigationView.setSelectedItemId(R.id.tab_news);
    }

    private Fragment getContent(MenuItem menuItem) {
        int id = menuItem.getItemId();
        Fragment fragment = mFragmentCache.get(id);
        if (fragment == null) {
            fragment = nf(menuItem);
            mFragmentCache.put(id, fragment);
        }
        return fragment;
    }

    /*
    底部导航栏
     */

    private Fragment nf(MenuItem menuItem) {
        Fragment fragment;
        switch (menuItem.getItemId()){
            case R.id.tab_user_center:
                top01.setVisibility(View.GONE);
                fragment =new MineFragment();

                break;
            case R.id.tab_circle:
//                top01.setVisibility(View.VISIBLE);
                fragment =new NewTopFragment();
                break;
            case R.id.tab_news:
                fragment =new NewTopFragment();
//                top01.setVisibility(View.VISIBLE);
                break;
            case R.id.tab_friend:
                fragment =new NewTopFragment();
//                top01.setVisibility(View.VISIBLE);
                break;
//            case R.id.tab_store:
//                fragment =new NewTopFragment();
////                top01.setVisibility(View.VISIBLE);
//                break;
            default:
                fragment =new NewTopFragment();
        }

        fragment.setArguments(NewTopFragment.newArgument(menuItem.getTitle().toString()));
        return fragment;
    }



    @Override
    protected void bindEvent() {
        settingBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast.makeText(getApplicationContext(), "跳转设置",Toast.LENGTH_SHORT).show();
                Intent intent = new Intent();
                    intent.setClass(MainActivity.this, Setting.class);
                    startActivity(intent);
            }
        });

        scanView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "跳转设置",Toast.LENGTH_SHORT).show();
            }
        });

        drawerBtn.setOnClickListener(v -> mDrawerLayout.openDrawer(GravityCompat.START));

        mDrawerLayout.addDrawerListener(new DrawerLayout.SimpleDrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                float diff = drawerView.getWidth() + drawerView.getX();
                rootContentView.setTranslationX(diff);
            }
        });

    }
}

